package com.lay.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lay.entity.Role;
import com.lay.entity.UserNumber;
import com.lay.service.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpSession;


@Controller
public class LoginController {

    @Autowired
    private UserNumberService userNumberService;
    @Autowired
    private RoleService roleService;
    @RequestMapping(value = "/")
    public ModelAndView tologin(){
        return  new ModelAndView("login");
    }
    @RequestMapping(value = "/index")
    public ModelAndView toindex(){
        return  new ModelAndView("index1");
    }
    @RequestMapping(value = "/login1",method = RequestMethod.POST)
    public ModelAndView login(UserNumber userNumber, HttpSession session){
        ModelAndView mv = new ModelAndView();
        Subject subject = SecurityUtils.getSubject();
        subject.login(new UsernamePasswordToken(userNumber.getUsername(),userNumber.getPassword()));
        QueryWrapper qw = new QueryWrapper();
        qw.eq("username",userNumber.getUsername());
        qw.eq("password",userNumber.getPassword());
        qw.eq("un_state",1);
        UserNumber userNumber1 = userNumberService.getOne(qw);
        if (userNumber1 != null){
            Integer rid = userNumber1.getrId();
            Role role = roleService.getById(rid);
            String powers = role.getPowers();
            session.setAttribute("users",userNumber1);
            session.setAttribute("UserPower",powers);
            mv.setViewName("index");
            return mv;
        }
     return null;
    }
    @RequestMapping("/towelcome")
    public ModelAndView welcome(){
        return new ModelAndView("welcome");
    }
    @RequestMapping("/tochart")
    public ModelAndView tochart(){
        return new ModelAndView("chart");
    }

}
